package com.dt76.smallloanbackenduser.controller.zjs;

import com.dt76.smallloanbackenduser.remote.zjs.UserRemote;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/zjs")
public class UserController {

    @Autowired
    private UserRemote userRemote;

    @RequestMapping("/User")

    public String getAll(Model model){
        model.addAttribute("qryUserAllList",userRemote.qryAllUser());

        System.out.println("調用遠程服務成功");
        System.out.println(userRemote.qryAllUser());
        return "pages/zjs/customerManagement";
    }


    @RequestMapping("/qryPageUserAll")

    public String qryPageAll(Model model,
                             @RequestParam(defaultValue = "1")Integer currentPageNo,
                             @RequestParam(value = "queryname",required = false)String queryname,
                             @RequestParam(value = "queryTime",required = false)String queryTime,
                             @RequestParam(value = "userId",required = false)String userId) {



//        if (currentPageNo=="" || currentPageNo==null){
//            currentPageNo = "1";
//        }

        if (currentPageNo<=0){
            currentPageNo=1;
        }
        //02.调用动态查询+分页查询接口
        String paramName = "";
        if(queryname != null && !queryname.equals("")){
            paramName = "%"+queryname+"%";
        }
        String paramTime = "";
        if(queryTime != null && !queryTime.equals("")){
            paramTime = "queryTime";
        }
        if( userId == null || userId.equals("") ){
            userId = "0";
        }

        Page page = userRemote.qryPageAllUser(Integer.valueOf(currentPageNo), paramName, Integer.valueOf(userId),paramTime);

        //03.组织返回值
        model.addAttribute("queryPageUserList", page.getRows());//用户列表
        model.addAttribute("page", page);
        //加入动态查询条件返回
        model.addAttribute("queryname",queryname);
        model.addAttribute("paramTime",paramTime);
//        model.addAttribute("queryuserId",queryUserId);

        System.out.println(page.getRows());
        System.out.println(page);
        return "pages/zjs/customerManagement";
    }




        @RequestMapping("/delUser/{userId}")
        @ResponseBody
        public Result delete(@PathVariable String userId){


            return userRemote.delUser(Integer.parseInt(userId));
        }

}
